/*
 * @Author: fengYM feng25321@163.com
 * @Date: 2023-09-25 10:34:21
 * @LastEditors: fengYM feng25321@163.com
 * @LastEditTime: 2023-10-12 16:57:57
 * @Description:api.ts
 */
export const BASE_URL = "/k3cloud"; // 你的接口基础URL
export const BASE_UPLOADURL = BASE_URL + "/uploadHeadImg.do";
// 统一的异常处理函数
function handleRequestError(error: any) {
  uni.showToast({
    title: error,
    icon: "none",
    duration: 2000,
  });
}
// 发送请求的封装
export function apiRequest(config: any): any {
  config.url = `${BASE_URL}${config.url}`;
  config.data = {
    ...config.data,
  };
  // config.header = { "Content-Type": "application/x-www-form-urlencoded" };
  uni.showLoading({
    title: "加载中",
  });
  return new Promise((resolve, reject) => {
    uni.request({
      ...config,
      success(response: { data: any }) {
        uni.hideLoading();
        const data = response.data;
        if (data?.Result?.ResponseStatus.IsSuccess || data.length >= 0 || data.IsSuccessByAPI) {
          resolve(data);
        } else {
          if (data.Result.ResponseStatus.MsgCode === 0) return;
          handleRequestError(data.Result.ResponseStatus.Errors[0].Message);
          reject(new Error(data));
        }
      },
      fail(error: any) {
        uni.hideLoading();
      },
      complete() {
        //隐藏loading
      },
    });
  });
}
